#include "DXUT.h"
#include "WNDDialogLoadXYZ.h"

CWNDDialogLoadXYZ::CWNDDialogLoadXYZ(void):CWNDDialogUsr(),m_iXYZWidth(140),m_iXYZHeight(110),m_bFileSelected(false)
{
}


CWNDDialogLoadXYZ::~CWNDDialogLoadXYZ(void)
{
}

BOOL CWNDDialogLoadXYZ::ShowDialog(LPCTSTR lpTemplate)
{
	if (NULL == m_dlgWnd)
	{
		if( Create(lpTemplate) )
		{
			printf("ReCreate ToolBar Again!\n");
			ShowWindow(m_dlgWnd, SW_SHOW);
			return TRUE;
		}
		else
		{
			printf("Failed to Recreate ToolBar!\n");
			return FALSE;
		}
	}
	else
	{
		ShowWindow(m_dlgWnd, SW_SHOW);
		return TRUE;
	}
}

BOOL CWNDDialogLoadXYZ::Create(LPCTSTR lpTemplate)
{
	if(NULL == m_dlgWnd)
		//test
	{
		HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
		//return	DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG1), DXUTGetHWNDFocus(), DlgProc);//modal dialog
		m_dlgWnd = CreateDialog(hInstance,lpTemplate, DXUTGetHWNDFocus(), DlgProc); //modeless dialog
		if (NULL == m_dlgWnd)
		{
			printf("LoadXYZ Dialog Create Failed!\n");
			return FALSE;
		}
		else
		{
			printf("LoadXYZ Dialog Create Succeeded!\n");
			SetWindowLongPtr(m_dlgWnd, GWL_USERDATA, (LONG_PTR)this);
			return TRUE;
		}
	}
	else
	{
		printf("LoadXYZ Dialog already Exists!\n");
		return TRUE;
	}
}

BOOL CALLBACK CWNDDialogLoadXYZ::DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		{
			TCHAR szBuffer[MAX_PATH];
			ZeroMemory(szBuffer,sizeof(TCHAR)*MAX_PATH);
			swprintf_s(szBuffer,L"%d",140);
			HWND hXYZWidth = GetDlgItem (hDlg, IDC_EDIT_XYZWIDTH);
	        //SendMessage(hXYZWidth, WM_SETTEXT, 0, (LPARAM) ((LPWSTR) szBuffer));
			SetWindowText(hXYZWidth,szBuffer);

			ZeroMemory(szBuffer,sizeof(TCHAR)*MAX_PATH);
			swprintf_s(szBuffer,L"%d",110);
			HWND hXYZHeight = GetDlgItem (hDlg, IDC_EDIT_XYZHEIGHT);
			SetWindowText(hXYZHeight,szBuffer);

			//center window
			SetWindowPos(hDlg,0,500,300,0,0,SWP_NOOWNERZORDER|SWP_NOSIZE);
			return TRUE;
		}
	break;
	
	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDC_BUTTON_SELXYZ:
			{
				OPENFILENAME opf;
				ZeroMemory( &opf, sizeof(opf));
				TCHAR filenamestring[MAX_PATH];
				memset(filenamestring,0,sizeof(TCHAR)*MAX_PATH);

				opf.hwndOwner = 0;
				opf.lpstrFilter = L"XYZ Files (*.xyz)\0*.xyz\0All Files (*.*)\0*.*\0";
				opf.lpstrCustomFilter = 0;
				opf.nMaxCustFilter = 0L;
				opf.nFilterIndex = 1L;
				opf.lpstrFile = filenamestring;
				opf.nMaxFile = 256;
				opf.lpstrFileTitle = 0;
				opf.nMaxFileTitle=50;
				opf.lpstrInitialDir = NULL;
				opf.lpstrTitle = L"Open File";
				opf.nFileOffset = 0;
				opf.nFileExtension = 0;
				opf.lpstrDefExt = L"*.*";
				opf.lpfnHook = NULL;
				opf.lCustData = 0;
				opf.Flags = (OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT) & ~OFN_ALLOWMULTISELECT;
				opf.lStructSize = sizeof(OPENFILENAME);

				if(GetOpenFileName(&opf))
				{
					HWND hXYZFileName = GetDlgItem(hDlg, IDC_STATIC_XYZFILENAME);
					SetWindowText(hXYZFileName,opf.lpstrFile);
					CWNDDialogLoadXYZ* pThis = (CWNDDialogLoadXYZ*)GetWindowLongPtr(hDlg,GWL_USERDATA);
					pThis->m_bFileSelected = true;
				}
				
				return TRUE;
			}
		case IDOK:
			{
				CWNDDialogLoadXYZ* pThis = (CWNDDialogLoadXYZ*)GetWindowLongPtr(hDlg,GWL_USERDATA);

				if(!pThis->m_bFileSelected)
				{
					printf("Select XYZFile Firstly!\n");
					return FALSE;
				}
				
				if(pThis->m_pHMTech == NULL)
					return FALSE;

				unsigned int iWidth, iHeight;				
				TCHAR szBuffer[MAX_PATH];
				ZeroMemory(szBuffer,sizeof(TCHAR)*MAX_PATH);
				HWND hXYZWidth = GetDlgItem (hDlg, IDC_EDIT_XYZWIDTH);
				GetWindowText(hXYZWidth,szBuffer,MAX_PATH);
				iWidth = _wtoi(szBuffer);

				ZeroMemory(szBuffer,sizeof(TCHAR)*MAX_PATH);
				HWND hXYZHeight = GetDlgItem (hDlg, IDC_EDIT_XYZHEIGHT);
				GetWindowText(hXYZHeight,szBuffer,MAX_PATH);
				iHeight = _wtoi(szBuffer);

				if(iWidth<=0||iHeight<=0)
					return FALSE;
				
				ZeroMemory(szBuffer,sizeof(TCHAR)*MAX_PATH);
				HWND hXYZFileName = GetDlgItem(hDlg, IDC_STATIC_XYZFILENAME);
				GetWindowText(hXYZFileName,szBuffer,MAX_PATH);
				
				char tmpStrFileName[MAX_PATH];
				ZeroMemory(tmpStrFileName,sizeof(char)*MAX_PATH);
				WideCharToMultiByte(CP_ACP,0,szBuffer,-1,tmpStrFileName,wcslen(szBuffer),NULL,NULL);
				printf("Load XYZFile: %s\n",tmpStrFileName);
				
				if(pThis->m_pHMTech->LoadXYZFile(tmpStrFileName,iWidth,iHeight))				
				{
					pThis->m_iXYZWidth = iWidth;
					pThis->m_iXYZHeight = iHeight;
					return TRUE;
				}
				else
					return FALSE;
			}

		case 	IDCANCEL:
			{
				EndDialog (hDlg, TRUE) ;
				return TRUE ;
			}
		}
	}
	return FALSE;
}
